package ve.usb.ddp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.ddp.hibernate.*;


/**
 * Manejo de elementos de la entidad PaiApoderado.<br>
 */
public class AccionesPaiApoderado extends CohesionAction {
    
/**
     * Called by Struts for the execution of action APreBuscar_paiApoderado.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreBuscar_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VBuscar_paiApoderado", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiApoderadoBuscar.clear(mapping, request);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreCrear_paiApoderado.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrear_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrear_paiApoderado", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiApoderado fFPaiApoderado = FPaiApoderado.clear(mapping, request);

             //Feed for combo listPaiResolucion.
             {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
                (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
                "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
                "from PaiResolucion").list();
              list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
              fFPaiApoderado.setListPaiResolucion(list);
             }


            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ABuscar_paiApoderado.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ABuscar_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_paiApoderado", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiApoderadoBuscar fFPaiApoderadoBuscar = (FPaiApoderadoBuscar)form;
            request.getSession().setAttribute("ABuscar_paiApoderado_campo", "ci");
            request.getSession().setAttribute("ABuscar_paiApoderado_valor", "%" + fFPaiApoderadoBuscar.getCi() + "%");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreListar_paiApoderado.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreListar_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VListar_paiApoderado", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            String crit = (String)request.getSession().getAttribute("ABuscar_paiApoderado_campo");
            java.util.List<PaiApoderado> list;
            if (crit==null) {
                list = (java.util.List<PaiApoderado>)s.createQuery("from PaiApoderado").list();
            } else {
                String valor = (String)request.getSession().getAttribute("ABuscar_paiApoderado_valor");
                list = (java.util.List<PaiApoderado>)
                   s.createQuery("from PaiApoderado where "+crit+" like :valor")
                    .setString("valor", valor).list();
                request.getSession().removeAttribute("ABuscar_paiApoderado_campo");
                request.getSession().removeAttribute("ABuscar_paiApoderado_valor");
            }
            request.setAttribute("L_PaiApoderado", list);
            for (PaiApoderado paiApoderado:list) {
                if (paiApoderado.getResolucion()!=null) paiApoderado.getResolucion().getFec_Resolucion();
            }


            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APre_paiApoderado.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APre_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paiApoderado", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idApoderado = lookup_Long(request, "idApoderado");
            
            FPaiApoderado fFPaiApoderado = FPaiApoderado.clear(mapping, request);
            PaiApoderado paiApoderado = (PaiApoderado)s.load(PaiApoderado.class, idApoderado);
            fFPaiApoderado.setIdApoderado(paiApoderado.getIdApoderado());
            fFPaiApoderado.setCi(paiApoderado.getCi());
            if (paiApoderado.getResolucion()!=null) {
                fFPaiApoderado.setResolucion(paiApoderado.getResolucion().getIdResolucion());
            } else {
                fFPaiApoderado.setResolucion(0L);
            }
            fFPaiApoderado.setNombre(paiApoderado.getNombre());
            fFPaiApoderado.setApellido(paiApoderado.getApellido());
            fFPaiApoderado.setCorreo(paiApoderado.getCorreo());
            fFPaiApoderado.setTelefono(paiApoderado.getTelefono());

            //Feed for combo listPaiResolucion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
               "from PaiResolucion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaiApoderado.setListPaiResolucion(list);
            }

            request.getSession().setAttribute("idApoderado", idApoderado);
            request.getSession().setAttribute("idApoderado_super", idApoderado);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AAct_paiApoderado.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AAct_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paiApoderado", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiApoderado fFPaiApoderado = (FPaiApoderado)form;
            PaiApoderado paiApoderado = (PaiApoderado)s.load(PaiApoderado.class, fFPaiApoderado.getIdApoderado());
            paiApoderado.setCi(fFPaiApoderado.getCi());
            if (fFPaiApoderado.getResolucion()==0) paiApoderado.setResolucion(null);
            else if ((paiApoderado.getResolucion()==null) ||
                    (paiApoderado.getResolucion().getIdResolucion()!=fFPaiApoderado.getResolucion())) {
                PaiResolucion __obj = (PaiResolucion)s.load(PaiResolucion.class, fFPaiApoderado.getResolucion());
                paiApoderado.setResolucion(__obj);
            }
            paiApoderado.setNombre(fFPaiApoderado.getNombre());
            paiApoderado.setApellido(fFPaiApoderado.getApellido());
            paiApoderado.setCorreo(fFPaiApoderado.getCorreo());
            paiApoderado.setTelefono(fFPaiApoderado.getTelefono());


            //Feed for combo listPaiResolucion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
               "from PaiResolucion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaiApoderado.setListPaiResolucion(list);
            }

            request.getSession().setAttribute("idApoderado", paiApoderado.getIdApoderado());
            request.getSession().setAttribute("idApoderado_super", paiApoderado.getIdApoderado());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AAct_paiApoderado.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AElim_paiApoderado.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AElim_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_paiApoderado", "V_paiApoderado", };
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idApoderado = lookup_Long(request, "idApoderado");
            PaiApoderado paiApoderado = (PaiApoderado)s.load(PaiApoderado.class, idApoderado);
            s.delete(paiApoderado);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_paiApoderado.msg0"));
        }
        if (salida==1) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_paiApoderado.msg1"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ACrear_paiApoderado.
     * Registro de la creación de una PaiApoderado.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrear_paiApoderado(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_paiApoderado", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPaiApoderado fFPaiApoderado = (FPaiApoderado)form;
            PaiApoderado paiApoderado = new PaiApoderado();
            paiApoderado.setCi(fFPaiApoderado.getCi());
            if (fFPaiApoderado.getResolucion()==0) paiApoderado.setResolucion(null);
            else {
                PaiResolucion __obj = (PaiResolucion)s.load(PaiResolucion.class, fFPaiApoderado.getResolucion());
                paiApoderado.setResolucion(__obj);
            }
            paiApoderado.setNombre(fFPaiApoderado.getNombre());
            paiApoderado.setApellido(fFPaiApoderado.getApellido());
            paiApoderado.setCorreo(fFPaiApoderado.getCorreo());
            paiApoderado.setTelefono(fFPaiApoderado.getTelefono());

            s.save(paiApoderado);
            s.flush();
            fFPaiApoderado.setIdApoderado(paiApoderado.getIdApoderado());

            //Feed for combo listPaiResolucion.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(fec_Resolucion, idResolucion) " + 
               "from PaiResolucion").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPaiApoderado.setListPaiResolucion(list);
            }

            request.getSession().setAttribute("idApoderado", paiApoderado.getIdApoderado());
            request.getSession().setAttribute("idApoderado_super", paiApoderado.getIdApoderado());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("ACrear_paiApoderado.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }



}